<?php
/**
 * 首页处理
 * index.php      网站首页缓存处理文件
 * index_do.php   处理页面路由，并根据参加加载控制器
 * 若无须处理页面级缓存，可采取以下任意两种方式：
 * 1、将本文件中的USE_PAGE_CACHE的定义值设置为FALSE
 * 2、将index_do.php改名为index.php，即将index_do.php重命名为index.php后，覆盖本文件
 * 
 * @author chenchao
 * @version 1.0.0
 * @since 20110331
 * 
 */

date_default_timezone_set('Asia/Shanghai');

//设置服务器名称
define("SERVER_NAME", "achao.org");

//页面缓存保存位置
define("PAGE_CACHE_DIR", dirname(__FILE__) . "/data/file_cache/");
//页面缓存时间，单位：秒
define("PAGE_CACHE_MAX_EXPIRE", 600);
//是否使用页面缓存
define("USE_PAGE_CACHE", FALSE);

//需要缓存的controler
$cache_pages = array("mall");
//不需要缓存的controller和action
$denyCache = array('apps|adapter', 'apps|osapp');

function write_cache ($file)
{
    $data = ob_get_clean();
    header("WebServer: " . SERVER_NAME);
    file_put_contents($file, $data, LOCK_EX);
    echo $data;
}

function do_cache ($fileName)
{
    $current_time = time();
    $file = PAGE_CACHE_DIR . $fileName;
    $fileTime = null;
    if (! file_exists($file) ||
     ($fileTime = filemtime($file)) < ($current_time - PAGE_CACHE_MAX_EXPIRE)) {
        ob_start();
        register_shutdown_function('write_cache', $file);
    } else {
        if ((isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) &&
         array_shift(explode(';', $_SERVER['HTTP_IF_MODIFIED_SINCE'])) ==
         gmdate('D, d M Y H:i:s', $fileTime) . ' GMT')) {
            header("HTTP/1.1 304 Not Modified");
            header(
            "Last-modified: " . gmdate('D, d M Y H:i:s', $fileTime) . ' GMT');
            header(
            "Expires: " .
             gmdate('D, d M Y H:i:s', $fileTime + PAGE_CACHE_MAX_EXPIRE) . ' GMT');
            header("Content-type: text/html");
            header('Content-Length: 0');
            header("WebServer: " . SERVER_NAME);
            die();
        } else {
            header(
            "Last-modified: " . gmdate('D, d M Y H:i:s', $fileTime) . ' GMT');
            header(
            "Expires: " .
             gmdate('D, d M Y H:i:s', $fileTime + PAGE_CACHE_MAX_EXPIRE) . ' GMT');
            header("Content-type: text/html");
            header("WebServer: " . SERVER_NAME);
            //$gzip = strstr($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip');
            //$deflate = strstr($_SERVER['HTTP_ACCEPT_ENCODING'], 'deflate');
            //$encoding = $gzip ? 'gzip' : ($deflate ? 'deflate' : 'none');
            /*$content = "";
            if($encoding != "none")
            {                
                header ("Content-Encoding: " . $encoding);
                $gzipFile = $file . ".gzip";
                if(file_exists($gzipFile))
                {
                    $content = file_get_contents($gzipFile);
                }
                else
                {
                    $content = file_get_contents($file);
                    $content = gzencode($content, 9, $gzip ? FORCE_GZIP : FORCE_DEFLATE);
                    //file_put_contents($gzipFile, $content,LOCK_EX);//echo $gzipFile;die();
                    file_put_contents($gzipFile, $content,LOCK_EX);//echo $gzipFile;die();
                }
            }
            */
            header("Content-length:" . filesize($file));
            //header("Content-Disposition:attachment;filename=\"".$file."\"");
            //header("X-LIGHTTPD-send-file:$file");
            @readfile($file);
            die();
        }
    }
}

if (defined("USE_PAGE_CACHE") && USE_PAGE_CACHE) {
    if (empty($_SERVER["QUERY_STRING"]) && $_SERVER["PHP_SELF"] == "/index.php" &&
     $_SERVER['REQUEST_METHOD'] != 'POST') {
        do_cache("index");
    }
    if (in_array(isset($_GET['app']) ? $_GET['app'] : '', $cache_pages)) {
        if (count($_GET) == 1) {
            do_cache(isset($_GET["app"]) ? $_GET['app'] : 'mall');
        } else {
            $app = isset($_GET['app']) ? $_GET['app'] : '';
            $act = isset($_GET['act']) ? $_GET['act'] : '';
            ! in_array($app . '|' . $act, $denyCache) &&
             do_cache($app . '.' . $act . '_' . md5($_SERVER["QUERY_STRING"]));
        }
    }
    //function do_filter()
    {
        foreach ($_REQUEST as $paramKey => $paramValue) {
            if (strlen($paramKey) > 3) {
                $postfix = substr($paramKey, - 3);
                if ($postfix == "_id" && ! is_numeric($paramValue) &&
                 ! strstr($paramValue, ',')) {
                    //               header('Content-Type:text/html;charset= utf-8');                
                //               die('请不要进行恶意访问~~');
                }
            }
            if (is_string($paramValue) &&
             preg_match("/.*<\s*script.*script\s*>.*/i", $paramValue)) {
                header('Content-Type:text/html;charset= utf-8');
                die('请不要进行恶意访问~~');
            }
        }
        foreach ($_POST as $paramKey => $paramValue) {
            if (strlen($paramKey) > 3) {
                $postfix = substr($paramKey, - 3);
                if ($postfix == "_id" && ! is_numeric($paramValue)) {
                    // header('Content-Type:text/html;charset= utf-8');
                // die('请不要进行恶意访问~~');
                }
            }
            if (is_string($paramValue) &&
             preg_match("/.*<\s*script.*script\s*>.*/i", $paramValue)) {
                header('Content-Type:text/html;charset= utf-8');
                die('请不要进行恶意访问~~');
            }
        }
    }
    //do_filter(); 
}

//处理原始路由请求
require ('./index_do.php');
